import React, {useState, useRef} from 'react'
import {message} from 'antd'
import { PageHeaderWrapper } from '@ant-design/pro-layout'
import ProTable, { ProColumns, ActionType } from '@ant-design/pro-table'
import { connect } from 'umi'
import { queryTurnPositiveList,saveTurnPositiveInfo,regularBpm} from '../service'
import { BPMStatusEnum, resignStatusList } from '@/utils/constant'
import moment from 'moment'
import AddForm from './components/AddForm';
import store from 'store2';

const TableList = ({currentUser}) => {
  const actionRef = useRef();
  const [visible, setVisible] = useState(false);
  const [loading,setLoading] = useState(false);
  const [record,setRecord] = useState({});

  const addApply = (record) =>{
    setVisible(true);
    setRecord(record);
  };

  const handleCancel = () => {
    setVisible(false);
    setRecord({});
  };

  const handleSubmit = (values) => {
    setLoading(true);
    saveTurnPositiveInfo(values).then((res)=>{
      if(res.flag){
        regularBpm(values.oid).then(res => {
          if(res.flag){
            handleCancel();
            window.open(res.data);
            store.remove(`personnel-turnPositive-` + currentUser.id);
            actionRef.current.reload();
            message.success('转正申请新建成功！');
          }else{
            message.error(`转正Bpm错误：` + res.error);
          }
        });
      }else{
        message.error(res.error);
      }
      setLoading(false);
    })
  };

  const columns = [
    {
      title: '序号',
      render:(text,record,index)=>`${index+1}`,
      hideInSearch: true,
    },
    {
      title: '工号',
      hideInSearch: true,
      dataIndex: 'code',
      key:'code',
    },
    {
      title: '姓名',
      dataIndex: 'name',
      hideInSearch: true,
      key:'name'
    },
    {
      title: '公司',
      dataIndex: 'orgname',
      hideInSearch: true,
      key:'orgname'
    },
    {
      title: '部门',
      hideInSearch: true,
      dataIndex: 'unitname',
      key:'unitname'
    },
    {
      title: '岗位',
      dataIndex: 'jobname',
      hideInSearch: true,
      key:'jobname'
    },
    {
      title: '入职日期',
      dataIndex: 'hiredate',
      hideInSearch: true,
      render: (text) => {
        return text != '-' ? moment(text).format('YYYY-MM-DD') : '';
      },
    },
    {
      title: '培训日期',
      dataIndex: 'trainDate',
      hideInSearch: true,
      render: (text) => {
        return text != '-' ? moment(text).format('YYYY-MM-DD') : '';
      },
    },
    {
      title: '预计转正日期',
      dataIndex: 'regularDay',
      hideInSearch: true,
      render: (text) => {
        return text != '-' ? moment(text).format('YYYY-MM-DD') : '';
      },
    },
    {
      title: '审批状态',
      dataIndex: 'regularBpmStatus',
      key:'regularBpmStatus',
      hideInSearch: true,
      valueEnum: BPMStatusEnum
    },
    {
      title: '操作',
      dataIndex: 'option',
      valueType: 'option',
      render: (item, record) => (
        <>
          {record.regularBpmStatus === 0 && <a onClick={() => addApply(record)}>发起转正申请</a>}
          {(record.regularCheckUrl && record.regularBpmStatus !== 0) && <><a onClick={() => window.open(record.regularCheckUrl)}>审批详情</a></>}
        </>
      ),
    },
  ];

  return (
    <PageHeaderWrapper>
      <ProTable
        actionRef={actionRef}
        rowKey="oid"
        search={false}
        request={(params) => queryTurnPositiveList()}
        columns={columns}
      />
      <AddForm
        loading={loading}
        visible={visible}
        record={record}
        onCancel={handleCancel}
        onSubmit={handleSubmit}
      />
    </PageHeaderWrapper>
  )
}

export default connect(({user}) => ({
  currentUser: user.currentUser,
}))(TableList)
